package com.yongqi.duocai.controller;


import com.yongqi.duocai.entity.Goods;
import com.yongqi.duocai.service.GoodsService;
import com.yongqi.duocai.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 夏亮辉
 * @since 2021-06-19
 */
@RestController
@Api(tags = "GoodsController")
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/info")
    @ApiOperation("获取所有商品信息")
    public JsonResult suchAll(){
        JsonResult jsonResult = new JsonResult();
        List<Goods> goods = goodsService.list();
        if(goods.size() == 0 && goods.equals("null") && goods == null){
            jsonResult.setCode(601);
            jsonResult.setMessage("当前商品信息为空！！");
        }else {
            jsonResult.setCode(200);
            jsonResult.setData(goods);
        }
        return jsonResult;

    }



    @PostMapping("/add")
    @ApiOperation("添加商品信息")
    public JsonResult add(@RequestBody Goods goods){
        JsonResult jsonResult = new JsonResult();
        boolean res = goodsService.save(goods);
        if(res){
            jsonResult.setCode(200);
            jsonResult.setMessage("添加成功");
        }
        return jsonResult;
    }

}

